import {createRouter, createWebHashHistory} from "vue-router";

const routes = [
    {
        path: '/',
        name: 'PlatformOverview',
        component:()=>import('../components/PlatformOverview.vue')
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../components/Login.vue')
    },
    {
        path: '/register',
        name: 'register',
        component: () => import('../components/Register.vue')
    },
    {
        path: '/DataInfoEnterOfExperimentalCondition',
        name: 'DataInfoEnterOfExperimentalCondition',
        component:()=>import('../components/DataInfoEnterPage/DataInfoEnterOfExperimentalCondition.vue')
    },

    {
        path: '/DataInfoEnterOfExperimentalBatch',
        name: 'DataInfoEnterOfExperimentalBatch',
        component:()=>import('../components/DataInfoEnterPage/DataInfoEnterOfExperimentalBatch.vue')
    },
    {
        path: '/DataInfoEnterOfDataUpload',
        name: 'DataInfoEnterOfDataUpload',
        component:()=>import('../components/DataInfoEnterPage/DataInfoEnterOfDataUpload.vue')
    },
    {
        path: '/MyData/ExperimentSet',
        component:()=>import('@/components/MyData/ExperimentSet.vue')
    },
    {
        path: '/MyData/Batch',
        component:()=>import('@/components/MyData/Batch.vue')
    },
    {
        path: '/MyData/Experiment',
        component:()=>import('@/components/MyData/Experiment.vue')
    },
    {
        path: '/MyData/Image',
        component:()=>import('@/components/MyData/Image.vue')
    },
    {
        path: '/MyData/ImageFilesTable',
        component:()=>import('@/components/MyData/ImageFilesTable.vue')
    },
    {
        path: '/MyData/HeatTreatmentImages',
        component:()=>import('@/components/MyData/HeatTreatmentImages.vue')
    },
    {
        path: '/ChainData/ExperimentSet',
        component:()=>import('@/components/ChainData/ExperimentSet.vue')
    },
    {
        path: '/ChainData/Batch',
        component:()=>import('@/components/ChainData/Batch.vue')
    },
    {
        path: '/ChainData/Experiment',
        component:()=>import('@/components/ChainData/Experiment.vue')
    },
    {
        path: '/ChainData/Image',
        component:()=>import('@/components/ChainData/Image.vue')
    },
    {
        path: '/Grant',
        component:()=>import('@/components/Grant/Grant.vue')
    },
    {
        path: '/Grant/AuthorizedBatch',
        component:()=>import('@/components/Grant/AuthorizedBatch.vue')
    },
    {
        path: '/Grant/AuthorizedExperiment',
        component:()=>import('@/components/Grant/AuthorizedExperiment.vue')
    },
    {
        path: '/Grant/AuthorizedImage',
        component:()=>import('@/components/Grant/AuthorizedImage.vue')
    },
    {
        path: '/UploadExperimentSet',
        component:()=>import('@/components/Upload/UploadExperimentSet.vue')
    },
    {
        path: '/UploadBatch',
        component:()=>import('@/components/Upload/UploadBatch.vue')
    },
    {
        path: '/UploadExperiment',
        component:()=>import('@/components/Upload/UploadExperiment.vue')
    },
    {
        path: '/detail',
        component:()=>import('@/components/DataInfoManageSubpage/Detail.vue')
    },
    {
        path: '/test',
        component:()=>import('@/components/test.vue')
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes: routes,
})

//导航守卫：实现登陆后才能访问
//isLogin存储于sessionStorage中，登录后会写入
router.beforeEach((to,from,next)=>{

    let isLogin=sessionStorage.getItem("isLogin");

    if (to.fullPath==='/login'||to.fullPath==='/register'){
        //避免已经登录的用户再次前往登录页面
        //用户只能在打开页面或点击注销按钮后才能前往登录页面
        if (isLogin==='true'){
            next('/')
        }else {
            next();
        }
    }else {
        let isLogin= sessionStorage.getItem("isLogin");
        if (isLogin==='true'){
            next();
        }else {
            next('/login');
        }
    }
})

export default router